<template>
  <div>
    <div class="shopcart">
      <div class="content" @click="toggleList">
        <div class="content-left">
          <div class="logo-wrapper">
            <div class="logo" :class="{'highlight':totalCount>0}">
              <i class="icon-shopping_cart" :class="{'highlight':totalCount>0}"></i>
            </div>
            <div class="num" v-show="totalCount>0">{{totalCount}}</div>
          </div>
          <div class="price" :class="{'highlight':totalCount>0}">￥{{totalPrice}}</div>
          <div class="desc">另需配送费￥{{deliveryPrice}}元</div>
        </div>
        <div class="content-rigth" @click.stop="pay">
          <div class="pay" :class="payClass">{{payDesc}}</div>
        </div>
      </div>
      <div class="shopcart-list" v-show="isListShow">
        <transition name="fold">
          <div class="list-wrapper">
            <div class="list-header">
              <h1 class="title">购物车</h1>
              <span class="empty" @click="empty">清空</span>
            </div>
            <div class="list-content">
              <scroll ref="scroll">
                <ul>
                  <li class="food" v-for="(food,index) in selectFoods" :key="index">
                    <span class="name">{{food.name}}</span>
                    <div class="price">
                      <span>￥{{food.price*food.count}}</span>
                    </div>
                    <div class="cartcontrol-wrapper">
                      <cartControl :food="food"></cartControl>
                    </div>
                  </li>
                </ul>
              </scroll>
            </div>
          </div>
        </transition>
      </div>
    </div>
    <div class="list-mask" v-show="isListShow" @click="maskClick"></div>
  </div>
</template>

<script>
import cartControl from "../../../components/content/cartControl/cartControl";
import scroll from "../../../components/common/scroll/scroll";
export default {
  name: "shopCart",
  data() {
    return {
      fold: true
      //true 表示list展开
    };
  },
  components: { cartControl, scroll },
  props: {
    selectFoods: {
      type: Array,
      default() {
        return [];
      }
    },
    deliveryPrice: {
      type: Number
    },
    minPrice: {
      type: Number
    }
  },
  methods: {
    toggleList() {
      if (!this.totalCount) {
        return;
      }
      this.fold = !this.fold;
      this.$refs.scroll.refresh();
      //取反实现来回展开
    },
    empty() {
      this.selectFoods.forEach(food => {
        food.count = 0;
        this.fold = false;
      });
    },
    maskClick() {
      this.fold = true;
    },
    pay() {
      if (this.totalPrice < this.minPrice) {
        return;
      }
      window.alert("前往支付");
    }
  },
  computed: {
    totalPrice() {
      let total = 0;
      this.selectFoods.forEach(food => {
        total += food.price * food.count;
      });
      return total;
    },
    totalCount() {
      let count = 0;
      this.selectFoods.forEach(food => {
        count += food.count;
      });
      return count;
    },
    payDesc() {
      if (this.totalPrice === 0) {
        return `￥${this.minPrice}起送`;
      } else if (this.totalPrice < this.minPrice) {
        let diff = this.minPrice - this.totalPrice;
        return `还差￥${diff}起送`;
      } else {
        return "去结算";
      }
    },
    payClass() {
      if (this.totalPrice < this.minPrice) {
        return "not-enough";
      } else return "enough";
    },
    isListShow() {
      if (!this.totalCount) {
        //如果没有添加商品
        this.fold = true;
        //购物车折叠
        return false;
        //购物车不显示
      }
      let show = !this.fold;
      return show;
    }
  }
};
</script>

<style lang="stylus" scoped>
.shopcart
  position: fixed
  bottom: 0
  left: 0
  z-index: 50
  width: 100%
  height: 48px
  .content
    display: flex
    height: 48px
    background-color: #141d27
    font-size: 0
    .content-left
      flex: 1
      .logo-wrapper
        display: inline-block
        position: relative
        box-sizing: border-box
        top: -10px
        margin: 0 12px
        padding: 6px
        width: 56px
        height: 56px
        vertical-align: top
        border-radius: 50%
        background-color: #141d27
        .num
          position: absolute
          top: 0
          right: 0
          width: 24px
          height: 16px
          line-height: 16px
          text-align: center
          border-radius: 16px
          font-size: 9px
          font-weight: 700
          box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.4)
          background-color: red
          color: #fff
        .logo
          width: 100%
          height: 100%
          text-align: center
          border-radius: 50%
          background-color: #2b343c
          &.highlight
            background-color: rgb(0, 160, 220)
          .icon-shopping_cart
            font-size: 24px
            line-height: 44px
            color: #80858a
            &.highlight
              color: #ffffff
      .price
        display: inline-block
        vertical-align: top
        box-sizing: border-box
        margin-top: 12px
        padding-right: 12px
        border-right: 1px solid rgba(255, 255, 255, 0.1)
        line-height: 24px
        font-size: 16px
        font-weight: 700
        color: rgba(255, 255, 255, 0.4)
        &.highlight
          color: #ffffff
      .desc
        display: inline-block
        vertical-align: top
        margin: 12px 0 0 6px
        line-height: 24px
        font-size: 9px
        font-weight: 200
        color: rgba(255, 255, 255, 0.4)
    .content-rigth
      flex: 0 0 90px
      width: 105px
      background-color: #2b333b
      .pay
        height: 48px
        line-height: 48px
        text-align: center
        font-size: 12px
        color: rgba(225, 225, 225, 0.4)
        &.not-enough
          background-color: #2b333b
        &.enough
          background-color: #00b43c
          color: #ffffff
          font-weight: 700
  .shopcart-list
    position: absolute
    bottom: 48px
    left: 0
    z-index: -1
    width: 100%
    .fold
      transition: all 1s
      .fold-enter-active, .fold-leave-active
        transform: translate3d(0, -100%, 0)
      .fold-enter, .fold-leave-to
        transform: translate3d(0, 0, 0)
    .list-header
      height: 40px
      line-height: 40px
      padding: 0 18px
      background-color: #f3f5f7
      border-bottom: 1px solid rgba(7, 17, 27, 0.1)
      .title
        float: left
        font-size: 14px
        color: rgb(7, 17, 27)
      .empty
        float: right
        font-size: 12px
        color: rgb(0, 160, 220)
    .list-content
      padding: 0 18px
      max-height: 217px
      background-color: #fff
      overflow: hidden
      .wrapper
        max-height: 217px
      .food
        position: relative
        padding: 12px 0
        box-sizing: border-box
        border-bottom: 1px solid rgba(7, 17, 27, 0.1)
        .name
          line-height: 24px
          font-size: 14px
          color: rgb(7, 17, 27)
        .price
          position: absolute
          right: 90px
          bottom: 12px
          line-height: 24px
          font-size: 14px
          font-weight: 700
          color: red
        .cartcontrol-wrapper
          position: absolute
          right: 0
          bottom: 6px
.list-mask
  position: fixed
  top: 0
  left: 0
  width: 100%
  height: 100%
  z-index: 40
  background-color: rgba(7, 17, 27, 0.7)
  backdrop-filter: blur(10px)
</style>